package cn.iocoder.yudao.module.handguard.service.housingresource;

import cn.iocoder.yudao.module.handguard.controller.app.housingresource.vo.HousingAreaRespVO;
import com.baomidou.dynamic.datasource.annotation.Slave;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.handguard.controller.admin.housingresource.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.housingresource.HousingResourceDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.handguard.convert.housingresource.HousingResourceConvert;
import cn.iocoder.yudao.module.handguard.dal.mysql.housingresource.HousingResourceMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.handguard.enums.ErrorCodeConstants.*;

/**
 * 房源信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class HousingResourceServiceImpl implements HousingResourceService {

    @Resource
    private HousingResourceMapper housingResourceMapper;

    @Override
    public Long createHousingResource(HousingResourceCreateReqVO createReqVO) {
        // 插入
        HousingResourceDO housingResource = HousingResourceConvert.INSTANCE.convert(createReqVO);
        String url = housingResourceMapper.getSslURL("ssl.image.url");
        if (createReqVO.getCoverPicLists()!=null){
            String coverPic = "";
            for (CommUploadParam c : createReqVO.getCoverPicLists()) {
                String str = c.getUrl().replaceAll(url, "");
                coverPic += str + ",";
            }
            createReqVO.setCoverPic(coverPic);
        }
        if (createReqVO.getDetailLists()!=null){
            String detailPic = "";
            for (CommUploadParam c : createReqVO.getDetailLists()) {
                String str = c.getUrl().replaceAll(url, "");
                detailPic += str + ",";
            }
            createReqVO.setDetailPic(detailPic);
        }
        if (StringUtils.isNotEmpty(createReqVO.getBackgroundPic())){
            housingResource.setBackgroundPic(createReqVO.getBackgroundPic().replaceAll(url,""));
        }
        if (StringUtils.isNotEmpty(createReqVO.getInfoTable())){
            housingResource.setInfoTable(createReqVO.getInfoTable().replaceAll(url,""));
        }
        housingResource.setIsHutai("1");//系统后台新增
        housingResourceMapper.insert(housingResource);
        // 返回
        return housingResource.getId();
    }

    @Override
    public void updateHousingResource(HousingResourceUpdateReqVO updateReqVO) {
        // 校验存在
        validateHousingResourceExists(updateReqVO.getId());
        // 更新
        String url = housingResourceMapper.getSslURL("ssl.image.url");
        if (updateReqVO.getCoverPicLists()!=null){
            String coverPic = "";
            for (CommUploadParam c : updateReqVO.getCoverPicLists()) {
                String str = c.getUrl().replaceAll(url, "");
                coverPic += str + ",";
            }
            updateReqVO.setCoverPic(coverPic);
        }
        if (updateReqVO.getDetailLists()!=null){
            String detailPic = "";
            for (CommUploadParam c : updateReqVO.getDetailLists()) {
                String str = c.getUrl().replaceAll(url, "");
                detailPic += str + ",";
            }
            updateReqVO.setDetailPic(detailPic);
        }

        if (StringUtils.isNotEmpty(updateReqVO.getBackgroundPic())){
            updateReqVO.setBackgroundPic(updateReqVO.getBackgroundPic().replaceAll(url,""));
        }
        if (StringUtils.isNotEmpty(updateReqVO.getInfoTable())){
            updateReqVO.setInfoTable(updateReqVO.getInfoTable().replaceAll(url,""));
        }

        HousingResourceDO updateObj = HousingResourceConvert.INSTANCE.convert(updateReqVO);

        housingResourceMapper.updateById(updateObj);
    }

    @Override
    public void deleteHousingResource(Long id) {
        // 校验存在
        validateHousingResourceExists(id);
        // 删除
        housingResourceMapper.deleteById(id);
    }

    private void validateHousingResourceExists(Long id) {
        if (housingResourceMapper.selectById(id) == null) {
            throw exception(HOUSING_RESOURCE_NOT_EXISTS);
        }
    }

    @Override
    @Slave
    public HousingResourceDO getHousingResource(Long id) {
        HousingResourceDO resourceDO = housingResourceMapper.selectById(id);
        String url = housingResourceMapper.getSslURL("ssl.image.url");
        if (resourceDO!=null){
            if (StringUtils.isNoneEmpty(resourceDO.getDetailPic())){
                String [] imgs = resourceDO.getDetailPic().split(",");
                List<String> imgList = new ArrayList<>();
                for (String m : imgs){
                    imgList.add(url+m);
                }
                resourceDO.setDetailList(imgList);
            }
            if (StringUtils.isNoneEmpty(resourceDO.getCoverPic())){
                String [] imgs = resourceDO.getCoverPic().split(",");
                List<String> imgList = new ArrayList<>();
                for (String m : imgs){
                    imgList.add(url+m);
                }
                resourceDO.setCoverPicList(imgList);
            }
            if (StringUtils.isNotEmpty(resourceDO.getBackgroundPic()) && !resourceDO.getBackgroundPic().equals("null")){
                resourceDO.setBackgroundPic(url+resourceDO.getBackgroundPic());
            }else {
                if (StringUtils.isNotEmpty(resourceDO.getCoverPic())){
                    String [] arr = resourceDO.getCoverPic().split(",");
                    resourceDO.setBackgroundPic(url+arr[0]);
                }
            }
            if (StringUtils.isNotEmpty(resourceDO.getInfoTable()) && !resourceDO.getInfoTable().equals("null")){
                resourceDO.setInfoTable(url+resourceDO.getInfoTable());
            }

        }
        return resourceDO;
    }

    @Override
    @Slave
    public List<HousingResourceDO> getHousingResourceList() {
        List<HousingResourceDO> doList = housingResourceMapper.selectList();
        String url = housingResourceMapper.getSslURL("ssl.image.url");
        if (doList.size()>0){
            doList.forEach(d ->{
                if (StringUtils.isNoneEmpty(d.getDetailPic())){
                    String [] imgs = d.getDetailPic().split(",");
                    List<String> imgList = new ArrayList<>();
                    for (String m : imgs){
                        imgList.add(url+m);
                    }
                    d.setDetailList(imgList);
                }
                if (StringUtils.isNoneEmpty(d.getCoverPic())){
                    String [] imgs = d.getCoverPic().split(",");
                    List<String> imgList = new ArrayList<>();
                    for (String m : imgs){
                        imgList.add(url+m);
                    }
                    d.setCoverPicList(imgList);
                }
                if (StringUtils.isNotEmpty(d.getBackgroundPic()) && !d.getBackgroundPic().equals("null")){
                    d.setBackgroundPic(url+d.getBackgroundPic());
                }
                if (StringUtils.isNotEmpty(d.getInfoTable()) && !d.getInfoTable().equals("null")){
                    d.setInfoTable(url+d.getInfoTable());
                }
            });
        }
        return doList;
    }

    @Override
    @Slave
    public PageResult<HousingResourceDO> getHousingResourcePage(HousingResourcePageReqVO pageReqVO) {
        PageResult<HousingResourceDO> pageResult = housingResourceMapper.selectPage(pageReqVO);
        String url = housingResourceMapper.getSslURL("ssl.image.url");
        List<HousingResourceDO> doList = pageResult.getList();
        if (doList.size()>0){
            doList.forEach(d ->{
                if (StringUtils.isNoneEmpty(d.getDetailPic())){
                    String [] imgs = d.getDetailPic().split(",");
                    List<String> imgList = new ArrayList<>();
                    for (String m : imgs){
                        imgList.add(url+m);
                    }
                    d.setDetailList(imgList);
                }
                if (StringUtils.isNoneEmpty(d.getCoverPic())){
                    String [] imgs = d.getCoverPic().split(",");
                    List<String> imgList = new ArrayList<>();
                    for (String m : imgs){
                        imgList.add(url+m);
                    }
                    d.setCoverPicList(imgList);
                }
                if (StringUtils.isNotEmpty(d.getBackgroundPic()) && !d.getBackgroundPic().equals("null")){
                    d.setBackgroundPic(url+d.getBackgroundPic());
                }else {
                    if (StringUtils.isNotEmpty(d.getCoverPic())){
                        String [] arr = d.getCoverPic().split(",");
                        d.setBackgroundPic(url+arr[0]);
                    }
                }
                if (StringUtils.isNotEmpty(d.getInfoTable()) && !d.getInfoTable().equals("null")){
                    d.setInfoTable(url+d.getInfoTable());
                }

            });
        }
        return pageResult;
    }

    @Override
    public List<HousingResourceDO> getHousingResourceList(HousingResourceExportReqVO exportReqVO) {
        List<HousingResourceDO> doList = housingResourceMapper.selectList(exportReqVO);
        return doList;
    }

    @Override
    public void updateHousingMobile(HousingMobileReqVO reqVO) {
        housingResourceMapper.updateHousingMobile(reqVO.getMobile());
    }

    @Override
    public String getHousingMobile() {
        return housingResourceMapper.getHousingMobile();
    }

    @Override
    public String getHousingUpdateTime(String type) {
        return housingResourceMapper.getHousingUpdateTime(type);
    }

}
